using System;
using System.Collections.Generic;
using System.IO;
using Core;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace IglooCoder.Commons.WcfNhibernate
{
    public static class NHibernateFactory
    {
        private static ISessionFactory _sessionFactory;
        private static Configuration configuration;
        private const String _ConfigFileName = "hibernate.cfg.xml";

        public static void Initialize()
        {
            var model = FluentNHibernate.Automapping.AutoMap.AssemblyOf<Core.User>();

            model.Override<Core.User>(map =>
                                      map.Id(x => x.UserID).CustomSqlType("varchar(32)"));
            model.Override<Core.User>(map =>
                                      map.Table("Users"));
            model.Override<Core.User>(map =>
                   map.HasManyToMany(x => x.Roles).Table("UserRole").ParentKeyColumn("UserID").ChildKeyColumn("RoleID").LazyLoad());
           
          

            model.Override<Core.Role>(map =>
                                      map.Id(x => x.RoleId));
            model.Override<Core.Role>(map =>
                map.HasManyToMany(x => x.Menus).Table("MenuRole").ParentKeyColumn("RoleID").ChildKeyColumn("MenuID").LazyLoad());
            model.Override<Core.Role>(map =>
                map.HasManyToMany(x => x.Users).Table("UserRole").ParentKeyColumn("RoleID").ChildKeyColumn("UserID").LazyLoad());
          


            model.Override<Core.Menu>(map =>
                                     map.Table("Menus"));
            model.Override<Core.Menu>(map =>
                                      map.Id(x => x.MenuID));
            model.Override<Core.Menu>(map => map.References(x => x.ParentMenu).Column("ParentID").LazyLoad());
            model.Override<Core.Menu>(map =>
               map.HasManyToMany(x => x.Roles).Table("MenuRole").ParentKeyColumn("MenuID").ChildKeyColumn("RoleID").LazyLoad());
          

            model.Override<Core.Parameter>(map =>
                                     map.Id(x => x.id));

            model.Override<Core.Inventory>(map => map.Id(x => x.SerialNumber).Column("SerialNumber").CustomType("AnsiString").Length(100));
            model.Override<Core.Inventory>(map =>
                                         map.Table("inventory"));
            model.Override<Core.Inventory>(map =>
                                           map.References(x => x.Item).Column("ItemID").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.Inventory>(
                map => map.HasMany(x => x.InventoryWarehouses).KeyColumn("SerialNumber").Not.KeyUpdate().Fetch.Join());
          

            model.Override<Core.Inventory>(map => map.References(x => x.SatuanPack, "SatuanPack").Fetch.Join().LazyLoad());
            model.Override<Core.Inventory>(map => map.References(x => x.SatuanUnit, "SatuanUnit").Fetch.Join().LazyLoad());
            
     
            model.Override<Core.Group>(map => map.Table("Groups"));
            model.Override<Core.Group>(map => map.Id(x => x.GroupID).CustomType("AnsiString").Length(50));
            //model.Override<Core.Group>(map => map.References(x => x.SatuanPack).Fetch.Join().Column("SatuanPack").LazyLoad());
            //model.Override<Core.Group>(map => map.References(x => x.SatuanUnit).Fetch.Join().Column("SatuanUnit").LazyLoad());


            model.Override<Core.SubGroup>(map => map.Table("SubGroup"));
            model.Override<Core.SubGroup>(map => map.Id(x => x.SubGroupID).CustomType("AnsiString").Length(50));
            model.Override<Core.SubGroup>(map => map.References(x => x.Group).Fetch.Join().Not.Nullable().Column("GroupID").LazyLoad());
            //model.Override<Core.SubGroup>(map => map.HasMany(x => x.Items).KeyColumn("SubGroupID").LazyLoad());
         

            model.Override<Core.Item>(map => map.Table("Item"));
            model.Override<Core.Item>(map => map.Id(x => x.ItemId).CustomType("AnsiString").Length(50));
            model.Override<Core.Item>(map => map.References(x => x.SubGroup).Column("SubGroupID").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.Item>(map => map.References(x => x.Satuan).Fetch.Join().Column("KodeSatuan").LazyLoad());
            model.Override<Core.Item>(map => map.References(x => x.SatuanPack).Fetch.Join().Column("SatuanPack").LazyLoad());


            //model.Override<Core.Pallet>(map => map.Table("Pallet"));
            //model.Override<Core.Pallet>(map => map.Id(x => x.KodePallet));
            //model.Override<Core.Pallet>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").LazyLoad());

            model.Override<Core.Warehouse>(map => map.Table("Warehouse"));
            model.Override<Core.Warehouse>(map => map.Id(x => x.KodeWarehouse).CustomType("AnsiString").Length(50));
            model.Override<Core.Warehouse>(map => map.IgnoreProperty(x => x.InventoryWarehouses));

            model.Override<Core.Satuan>(map => map.Id(x => x.KodeSatuan).CustomType("AnsiString").Length(50));
            model.Override<Core.Currency>(map => map.Id(x => x.CurrencyCode));

            model.Override<Core.COA>(map => map.Id(x => x.AccountNumber));
            //model.Override<Core.COA>(map => map.References(x => x.Currency).Column("CurrencyCode").LazyLoad());
            model.Override<Core.COA>(map => map.References(x => x.ParentAccount).Column("ParentAccount").Nullable().LazyLoad());


            model.Override<Core.Supplier>(map => map.Id(x => x.SupplierId));
            model.Override<Core.Supplier>(map => map.Map(x => x.SupplierName).CustomType("AnsiString").Length(50));
            model.Override<Core.Supplier>(map => map.References(x => x.Coa).Column("AccountNumber").Nullable().LazyLoad());

            model.Override<Core.Customer>(map =>map.Id(x => x.CustomerId));
            model.Override<Core.Customer>(map => map.Map(x => x.CustomerName).CustomType("AnsiString").Length(50));
            model.Override<Core.Customer>(map => map.References(x => x.Coa).Column("AccountNumber").Nullable().LazyLoad());

            model.Override<Core.PurchaseOrder>(map => map.Id(x => x.PONumber).CustomType("AnsiString").Length(50));
            model.Override<Core.PurchaseOrder>(map => map.References(x => x.Supplier).Column("SupplierId").LazyLoad());
            model.Override<Core.PurchaseOrder>(map => map.References(x => x.Currency).Column("CurrencyCode").LazyLoad());
            model.Override<Core.PurchaseOrder>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").LazyLoad());
            model.Override<Core.PurchaseOrder>(
                map => map.HasMany(x => x.PurchaseOrderDetails).KeyColumn("PONumber").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.PurchaseOrder>(
              map => map.HasMany(x => x.Btbs).KeyColumn("PONumber").Cascade.None().LazyLoad());


            model.Override<Core.PurchaseOrderDetail>(map => map.Id(x => x.PurchaseOrderDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_purchaseorder", "1000"));
            model.Override<Core.PurchaseOrderDetail>(map => map.References(x => x.PurchaseOrder).Column("PONumber").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.PurchaseOrderDetail>(map => map.References(x => x.SubGroup).Column("SubGroupId").LazyLoad());


            model.Override<Core.BTB>(map => map.Id(x => x.NoBtb).CustomType("AnsiString").Length(50));
            model.Override<Core.BTB>(map => map.References(x => x.PurchaseOrder).Column("PONumber").LazyLoad());
            model.Override<Core.BTB>(
                map => map.HasMany(x => x.BtbDetails).KeyColumn("NoBtb").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.BTB>(map => map.References(x => x.Supplier).Column("SupplierId").LazyLoad());


            model.Override<Core.BTBDetail>(map => map.Id(x => x.BtbDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_btb", "1000"));
            model.Override<Core.BTBDetail>(map =>
                                          map.References(x => x.Item).Column("ItemId").Fetch.Join().LazyLoad());
            model.Override<Core.BTBDetail>(map =>
                                           map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());
            model.Override<Core.BTBDetail>(map =>
                                          map.References(x => x.SatuanPack).Column("SatuanPack").Fetch.Join().LazyLoad());
            model.Override<Core.BTBDetail>(map =>
                                          map.References(x => x.SatuanUnit).Column("SatuanUnit").Fetch.Join().LazyLoad());
            model.Override<Core.BTBDetail>(map => map.References(x => x.Btb).Column("NoBtb").Not.Nullable().Fetch.Join().LazyLoad());


            model.Override<Core.PurchaseInvoice>(map => map.Id(x => x.PINo).CustomType("AnsiString").Length(50));
            model.Override<Core.PurchaseInvoice>(map => map.References(x => x.Currency).Column("CurrencyCode").LazyLoad());
            model.Override<Core.PurchaseInvoice>(map => map.References(x => x.Supplier).Column("SupplierId").LazyLoad());
            model.Override<Core.PurchaseInvoice>(map => map.HasMany(x => x.Btbs).KeyColumn("PINo").LazyLoad());
            model.Override<Core.PurchaseInvoice>(
                map => map.HasMany(x => x.PurchaseInvoiceDetails).KeyColumn("PINo").Cascade.All().Inverse().LazyLoad());

            model.Override<Core.PurchaseInvoiceDetail>(map => map.Id(x => x.PurchaseInvoiceDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_purchaseinvoice", "1000"));
            model.Override<Core.PurchaseInvoiceDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());
            model.Override<Core.PurchaseInvoiceDetail>(map =>
                                          map.References(x => x.PurchaseInvoice).Column("PINo").Fetch.Join().LazyLoad());


            model.Override<Core.PurchaseReturn>(map => map.Id(x => x.PRNo).CustomType("AnsiString").Length(50));
            model.Override<Core.PurchaseReturn>(map => map.References(x => x.PurchaseInvoice).Column("PRNo").LazyLoad());
            model.Override<Core.PurchaseReturn>(
                map => map.HasMany(x => x.PurchaseReturnDetails).KeyColumn("PRNo").Cascade.All().LazyLoad());
            model.Override<Core.PurchaseReturn>(map => map.References(x => x.Supplier).Column("SupplierId").Fetch.Join().LazyLoad());
            model.Override<Core.PurchaseReturn>(map => map.References(x => x.PurchaseInvoice).Column("PINo").Fetch.Join().LazyLoad());
            model.Override<Core.PurchaseReturn>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());


            model.Override<Core.PurchaseReturnDetail>(map => map.Id(x => x.PurchaseReturnDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_purchasereturn", "1000"));
            model.Override<Core.PurchaseReturnDetail>(map =>
                                          map.References(x => x.Inventory).Column("NoLog").Fetch.Join().LazyLoad());
            model.Override<Core.PurchaseReturnDetail>(map =>
                                         map.References(x => x.Warehouse).Column("KodeWarehouse").Nullable().Fetch.Join().LazyLoad());



            model.Override<Core.SalesOrder>(map => map.Id(x => x.SONo).CustomType("AnsiString").Length(50));
            model.Override<Core.SalesOrder>(map => map.IgnoreProperty(x => x.PSNo));
            model.Override<Core.SalesOrder>(map => map.IgnoreProperty(x => x.SINo));
            model.Override<Core.SalesOrder>(map => map.IgnoreProperty(x => x.DONo));
            model.Override<Core.SalesOrder>(map => map.IgnoreProperty(x => x.CurrencyCode));
            model.Override<Core.SalesOrder>(map => map.References(x => x.Currency).Column("CurrencyCode").Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrder>(map => map.References(x => x.DeliveryType).Column("DeliveryCode").LazyLoad());
            model.Override<Core.SalesOrder>(map => map.References(x => x.PreSalesOrder).Column("PSNo").LazyLoad());
            model.Override<Core.SalesOrder>(map => map.References(x => x.CostCenter).Column("CostCenterId").Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrder>(map => map.References(x => x.Salesman).Column("KodeSalesman").Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrder>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrder>(
                map => map.HasMany(x => x.SalesOrderDetails).KeyColumn("SONo").Cascade.All().Inverse().LazyLoad());


            model.Override<Core.SalesOrderDetail>(map => map.Id(x => x.SalesOrderDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_salesorder","1000"));
            model.Override<Core.SalesOrderDetail>(map => map.References(x => x.SubGroup).Column("SubGroupId").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrderDetail>(map => map.References(x => x.Item).Column("ItemId").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrderDetail>(map => map.References(x => x.Inventory).Column("SerialNumber").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SalesOrderDetail>(map => map.References(x => x.SalesOrder).Column("SONo").Not.Nullable().Fetch.Join().LazyLoad());

            model.Override<Core.DeliveryType>(map => map.Id(x => x.DeliveryCode));

            model.Override<Core.DeliveryOrder>(map => map.Id(x => x.DONo).CustomType("AnsiString").Length(50));
            model.Override<Core.DeliveryOrder>(map => map.IgnoreProperty(x => x.PSNo));
            model.Override<Core.DeliveryOrder>(map => map.IgnoreProperty(x => x.SONo));
            model.Override<Core.DeliveryOrder>(map => map.IgnoreProperty(x => x.SINo));
            model.Override<Core.DeliveryOrder>(map => map.References(x => x.SalesOrder).Column("SONo").LazyLoad());
            model.Override<Core.DeliveryOrder>(map => map.References(x => x.Spk).Column("NoSPK").LazyLoad());
            model.Override<Core.DeliveryOrder>(map => map.References(x => x.DeliveryType).Column("DeliveryCode").Fetch.Select().LazyLoad());
            model.Override<Core.DeliveryOrder>(map => map.References(x => x.CostCenter).Column("CostCenterId").Fetch.Join().LazyLoad());
            model.Override<Core.DeliveryOrder>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());
            model.Override<Core.DeliveryOrder>(
                map => map.HasMany(x => x.DeliveryOrderDetails).KeyColumn("DONo").Cascade.All().Inverse().LazyLoad());
            

            model.Override<Core.DeliveryOrderDetail>(map => map.Id(x => x.DeliveryOrderDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_deliveryorder","1000"));
            model.Override<Core.DeliveryOrderDetail>(map =>
                                          map.References(x => x.Inventory).Column("NoLog").Fetch.Join().LazyLoad());
            model.Override<Core.DeliveryOrderDetail>(map =>
                                         map.References(x => x.Warehouse).Column("KodeWarehouse").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.DeliveryOrderDetail>(map =>
                                         map.References(x => x.DeliveryOrder).Column("DONo").Not.Nullable().Fetch.Join().LazyLoad());


            model.Override<Core.SalesInvoice>(map => map.Id(x => x.SINo).CustomType("AnsiString").Length(50));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.NamaDepan));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.CustomerName));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.PSNo));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.SONo));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.DONo));
            model.Override<Core.SalesInvoice>(map => map.IgnoreProperty(x => x.CurrencyCode));
            model.Override<Core.SalesInvoice>(map => map.References(x => x.Customer).Column("CustomerId").LazyLoad().Fetch.Join());
            model.Override<Core.SalesInvoice>(map => map.References(x => x.Currency).Column("CurrencyCode").LazyLoad().Fetch.Join());
            model.Override<Core.SalesInvoice>(map => map.References(x => x.DeliveryOrder).Column("DONo").LazyLoad().Fetch.Join());
            model.Override<Core.SalesInvoice>(map => map.References(x => x.CostCenter).Column("CostCenterId").Fetch.Join().LazyLoad());
            model.Override<Core.SalesInvoice>(map => map.References(x => x.Salesman).Column("KodeSalesman").Fetch.Join().LazyLoad());
            model.Override<Core.SalesInvoice>(
                map => map.HasMany(x => x.SalesInvoiceDetails).KeyColumn("SINo").Cascade.All().Inverse().LazyLoad());


            model.Override<Core.SalesInvoiceDetail>(map => map.Id(x => x.SalesInvoiceDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_salesinvoice","1000"));
            model.Override<Core.SalesInvoiceDetail>(map => map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());
            model.Override<Core.SalesInvoiceDetail>(map => map.References(x => x.SalesInvoice).Column("SINo").Fetch.Join().LazyLoad());

            model.Override<Core.SalesReturn>(map => map.Id(x => x.SRNo).CustomType("AnsiString").Length(50));
            model.Override<Core.SalesReturn>(map => map.References(x => x.SalesInvoice).Column("SINo").LazyLoad());
            model.Override<Core.SalesReturn>(map => map.References(x => x.Customer).Column("CustomerId").LazyLoad());
            model.Override<Core.SalesReturn>(map => map.References(x => x.CostCenter).Column("CostCenterId").LazyLoad());
            model.Override<Core.SalesReturn>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").LazyLoad());
            model.Override<Core.SalesReturn>(
                map => map.HasMany(x => x.SalesReturnDetails).KeyColumn("SRNo").Cascade.All().Inverse().LazyLoad());

            model.Override<Core.SalesReturnDetail>(map => map.Id(x => x.SalesReturnDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_salesreturn","1000"));
            model.Override<Core.SalesReturnDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());
            //model.Override<Core.SalesReturnDetail>(map =>
            //                              map.References(x => x.Pallet).Column("KodePallet").Fetch.Join().LazyLoad());
            model.Override<Core.SalesReturnDetail>(map =>
                                           map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());
            model.Override<Core.SalesReturnDetail>(map =>
                                          map.References(x => x.SalesReturn).Column("SRNo").Fetch.Join().LazyLoad());


            model.Override<Core.MutasiWarehouse>(map => map.Id(x => x.NoMutasi).CustomType("AnsiString").Length(50));
            model.Override<Core.MutasiWarehouse>(map => map.References(x => x.FromWarehouse).Column("FromWarehouse").LazyLoad());
            model.Override<Core.MutasiWarehouse>(map => map.References(x => x.ToWarehouse).Column("ToWarehouse").LazyLoad());
            model.Override<Core.MutasiWarehouse>(
              map => map.HasMany(x => x.MutasiWarehouseDetails).KeyColumn("NoMutasi").Cascade.All().LazyLoad());


            model.Override<Core.MutasiWarehouseDetail>(map => map.Id(x => x.MutasiWarehouseDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_mutasiwarehouse","1000"));
            model.Override<Core.MutasiWarehouseDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());

            //model.Override<Core.MutasiPallet>(map => map.Id(x => x.NoMutasi));
            //model.Override<Core.MutasiPallet>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").LazyLoad());
            //model.Override<Core.MutasiPallet>(map => map.References(x => x.FromPallet).Column("FromPallet").LazyLoad());
            //model.Override<Core.MutasiPallet>(map => map.References(x => x.ToPallet).Column("ToPallet").LazyLoad());
            //model.Override<Core.MutasiPallet>(
            //  map => map.HasMany(x => x.MutasiPalletDetails).KeyColumn("NoMutasi").Cascade.All().LazyLoad());


            model.Override<Core.MutasiPalletDetail>(map => map.Id(x => x.MutasiPalletDetailId).GeneratedBy.Identity());
            model.Override<Core.MutasiPalletDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());


            model.Override<Core.BtbElse>(map => map.Id(x => x.NoBtb).CustomType("AnsiString").Length(50));
            model.Override<Core.BtbElse>(map => map.References(x => x.PurchaseOrder).Column("PONumber").LazyLoad());
            model.Override<Core.BtbElse>(
                map => map.HasMany(x => x.BtbDetails).KeyColumn("NoBtb").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.BtbElse>(map => map.References(x => x.Supplier).Column("SupplierId").LazyLoad());


            model.Override<Core.BtbElseDetail>(map => map.Id(x => x.BtbDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_btbelse","1000"));
            model.Override<Core.BtbElseDetail>(map =>
                                         map.References(x => x.Item).Column("ItemId").Fetch.Join().LazyLoad());
            model.Override<Core.BtbElseDetail>(map =>
                                           map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());
            model.Override<Core.BtbElseDetail>(map =>
                                          map.References(x => x.SatuanPack).Column("SatuanPack").Fetch.Join().LazyLoad());
            model.Override<Core.BtbElseDetail>(map =>
                                          map.References(x => x.SatuanUnit).Column("SatuanUnit").Fetch.Join().LazyLoad());
            model.Override<Core.BtbElseDetail>(map =>
                                          map.References(x => x.Btb).Column("NoBtb").Fetch.Join().Fetch.Join().LazyLoad());


            model.Override<Core.DeliveryOrderElse>(map => map.Id(x => x.DONo).CustomType("AnsiString").Length(50));
            model.Override<Core.DeliveryOrderElse>(map => map.References(x => x.DeliveryType).Column("DeliveryCode").LazyLoad());
            model.Override<Core.DeliveryOrderElse>(
                map => map.HasMany(x => x.DeliveryOrderDetails).KeyColumn("DONo").Cascade.All().LazyLoad());


            model.Override<Core.DeliveryOrderElseDetail>(map => map.Id(x => x.DeliveryOrderDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_deliveryorderelse", "1000"));
            model.Override<Core.DeliveryOrderElseDetail>(map =>
                                          map.References(x => x.Inventory).Column("NoLog").Fetch.Join().LazyLoad());
            model.Override<Core.DeliveryOrderElseDetail>(map =>
                                         map.References(x => x.Warehouse).Column("KodeWarehouse").Nullable().Fetch.Join().LazyLoad());



            model.Override<Core.PreSalesOrder>(map => map.Id(x => x.PSNo).CustomType("AnsiString").Length(50));
            model.Override<Core.PreSalesOrder>(
                map => map.IgnoreProperty(x => x.SONo));
            model.Override<Core.PreSalesOrder>(
                map => map.IgnoreProperty(x => x.SINo));
            model.Override<Core.PreSalesOrder>(
               map => map.IgnoreProperty(x => x.DONo));
            model.Override<Core.PreSalesOrder>(
                map => map.HasMany(x => x.PreSalesOrderDetails).KeyColumn("PSNo").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.PreSalesOrder>(map => map.References(x => x.Warehouse).Column("KodeWarehouse").Fetch.Join().LazyLoad());


            model.Override<Core.PreSalesOrderDetail>(map => map.Id(x => x.PreSalesOrderDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_presalesorder","1000"));
            model.Override<Core.PreSalesOrderDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());
            model.Override<Core.PreSalesOrderDetail>(map =>
                                          map.References(x => x.PreSalesOrder).Column("PSNo").Not.Nullable().Fetch.Join().LazyLoad());

           
            model.Override<Core.Jurnal>(map => map.Id(x => x.NoVoucher));
            model.Override<Core.Jurnal>(
                map => map.HasMany(x => x.JurnalDetails).KeyColumn("NoVoucher").Cascade.All().Inverse().LazyLoad());
           
            model.Override<Core.JurnalDetail>(map => map.Id(x => x.JurnalDetailId).GeneratedBy.GuidComb());
            model.Override<Core.JurnalDetail>(map =>
                                      map.References(x => x.Jurnal).Column("NoVoucher").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.JurnalDetail>(map =>
                                      map.References(x => x.Departemen).Column("CodeDepartemen").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.JurnalDetail>(map =>
                                    map.References(x => x.Supplier).Column("SupplierId").Nullable().LazyLoad());
            model.Override<Core.JurnalDetail>(map =>
                                  map.References(x => x.Customer).Column("CustomerId").Nullable().LazyLoad());

           
            model.Override<Core.JurnalConfig>(map => map.Id(x => x.JurnalConfigId).GeneratedBy.Identity());

            model.Override<Core.MutasiStok>(map => map.Id(x => x.MutasiStokId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_mutasistok", "1000"));
            model.Override<Core.MutasiStok>(map => map.Map(x => x.SerialNumber).CustomType("AnsiString").Length(100));
            model.Override<Core.MutasiStok>(map => map.Map(x => x.KodeWarehouse).CustomType("AnsiString").Length(50));

            model.Override<Core.InventoryAdjustment>(map => map.Id(x => x.NoAdj).CustomType("AnsiString").Length(50));
            model.Override<Core.InventoryAdjustment>(
                map => map.HasMany(x => x.InventoryAdjustmentDetails).KeyColumn("NoAdj").Cascade.All().Inverse().LazyLoad());

            model.Override<Core.InventoryAdjustmentDetail>(map => map.Id(x => x.InventoryAdjustmentDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_inventoryadjustment","1000"));
            model.Override<Core.InventoryAdjustmentDetail>(map =>
                                          map.References(x => x.Inventory).Column("SerialNumber").Fetch.Join().LazyLoad());
            model.Override<Core.InventoryAdjustmentDetail>(map =>
                                         map.References(x => x.Warehouse).Column("KodeWarehouse").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.InventoryAdjustmentDetail>(map =>
                                        map.References(x => x.InventoryAdjustment).Column("NoAdj").Not.Nullable().Fetch.Join().LazyLoad());


            model.Override<Core.AppParam>(map => map.Id(x => x.AppParamId));

            model.Override<Core.CostCenter>(map => map.Id(x => x.CostCenterId).CustomType("AnsiString").Length(15));
            model.Override<Core.CostCenter>(map => map.References(x => x.Departemen).Column("CodeDepartemen").Fetch.Join().LazyLoad());
            //model.Override<Core.MutasiStokWarehouse>(map => map.Id(x => x.MutasiStokWarehouseId).GeneratedBy.Identity());

            //model.Override<Core.InventoryWarehouse>(
            //    map => map.Id(x=>x.InventoryWarehouseId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_inventorywarehouse","30000"));
            model.Override<Core.InventoryWarehouse>(map=>map.Table("uvw_mutasi_stok"));
            model.Override<Core.InventoryWarehouse>(map => map.Id(x=>x.InventoryWarehouseId).Column("MutasiStokId"));
            model.Override<Core.InventoryWarehouse>(map =>map.Map(x=>x.Qty,"QtySisa"));
            model.Override<Core.InventoryWarehouse>(map => map.Map(x => x.Sheet, "SheetSisa"));
            model.Override<Core.InventoryWarehouse>(map => map.Map(x => x.KodePallet, "Pallet"));
            model.Override<Core.InventoryWarehouse>(map => map.IgnoreProperty(x => x.Panjang));
            model.Override<Core.InventoryWarehouse>(map => map.IgnoreProperty(x => x.Lebar));
            model.Override<Core.InventoryWarehouse>(map => map.Map(x => x.SerialNumber).CustomType("AnsiString").Length(100));
            model.Override<Core.InventoryWarehouse>(map => map.Map(x => x.KodeWarehouse).CustomType("AnsiString").Length(50));

            model.Override<Core.Spk>(map => map.Id(x => x.NoSPK).CustomType("AnsiString").Length(50));
            model.Override<Core.Spk>(map => map.IgnoreProperty(x => x.SINo));
            model.Override<Core.Spk>(map => map.IgnoreProperty(x => x.DONo));
            //model.Override<Core.Spk>(map => map.References(x => x.Customer).Column("CustomerId").Fetch.Join().LazyLoad());
            model.Override<Core.Spk>(map => map.References(x => x.Currency).Column("CurrencyCode").Fetch.Join().LazyLoad());
            model.Override<Core.Spk>(map => map.References(x => x.DeliveryType).Column("DeliveryCode").Fetch.Join().LazyLoad());
            model.Override<Core.Spk>(map => map.References(x => x.CostCenter).Column("CostCenterId").Fetch.Join().LazyLoad());
            model.Override<Core.Spk>(map => map.References(x => x.Salesman).Column("KodeSalesman").Fetch.Join().LazyLoad());

            model.Override<Core.Spk>(
                map => map.HasMany(x => x.SpkDetails).KeyColumn("NoSPK").Cascade.All().Inverse().LazyLoad());


            model.Override<Core.SpkDetail>(map => map.Id(x => x.SpkDetailId).GeneratedBy.HiLo("hibernate_unique_key", "next_hi_spk","1000"));
            model.Override<Core.SpkDetail>(map => map.References(x => x.SubGroup).Column("SubGroupId").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SpkDetail>(map => map.References(x => x.Item).Column("ItemId").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SpkDetail>(map => map.References(x => x.Inventory).Column("SerialNumber").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.SpkDetail>(map => map.References(x => x.Spk).Column("NoSPK").Not.Nullable().Fetch.Join().LazyLoad());


            model.Override<Core.Employee>(map => map.Id(x => x.KodeSalesman));
            model.Override<Core.Employee>(map => map.Table("Salesman"));

            model.Override<Core.Hpp>(map => map.Id(x => x.HppId).GeneratedBy.GuidComb());

            model.Override<Core.CashBankIn>(map => map.Id(x => x.NoVoucher).CustomType("AnsiString").Length(50));
            model.Override<Core.CashBankIn>(map => map.References(x => x.CashBankAccount).Column("AccountNumber").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankIn>(
              map => map.HasMany(x => x.CashBankInDetails).KeyColumn("NoVoucher").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.CashBankInDetail>(map => map.Id(x => x.CashBankInDetailId).GeneratedBy.GuidComb());
            model.Override<Core.CashBankInDetail>(map => map.References(x => x.CashBankIn).Column("NoVoucher").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankInDetail>(map => map.References(x => x.FromAccount).Column("AccountNumber").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.CashBankInDetail>(map => map.References(x => x.SalesInvoice).Column("SINo").Nullable().LazyLoad());
            model.Override<Core.CashBankInDetail>(map => map.References(x => x.Departemen).Column("CodeDepartemen").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.CashBankInDetail>(map => map.References(x => x.Customer).Column("CustomerId").Nullable().Fetch.Join().LazyLoad());

            model.Override<Core.CashBankOut>(map => map.Id(x => x.NoVoucher).CustomType("AnsiString").Length(50));
            model.Override<Core.CashBankOut>(map => map.References(x => x.CashBankAccount).Column("AccountNumber").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankOut>(
              map => map.HasMany(x => x.CashBankOutDetails).KeyColumn("NoVoucher").Cascade.All().Inverse().LazyLoad());
            model.Override<Core.CashBankOutDetail>(map => map.Id(x => x.CashBankInDetailId).GeneratedBy.GuidComb());
            model.Override<Core.CashBankOutDetail>(map => map.References(x => x.CashBankOut).Column("NoVoucher").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankOutDetail>(map => map.References(x => x.ToAccount).Column("AccountNumber").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankOutDetail>(map => map.References(x => x.PurchaseInvoice).Column("PINo").LazyLoad());
            model.Override<Core.CashBankOutDetail>(map => map.References(x => x.Departemen).Column("CodeDepartemen").Fetch.Join().LazyLoad());
            model.Override<Core.CashBankOutDetail>(map => map.References(x => x.Supplier).Column("SupplierId").Fetch.Join().LazyLoad());


            model.Override<Core.Departemen>(map => map.Id(x => x.Code).CustomType("AnsiString").Length(15));
            model.Override<Core.MutasiHutang>(map => map.Id(x => x.MutasiHutangId).GeneratedBy.GuidComb());
            model.Override<Core.MutasiPiutang>(map => map.Id(x => x.MutasiPiutangId).GeneratedBy.GuidComb());

            model.Override<Core.JurnalPembelian>(map => map.Id(x => x.NoVoucher));
            model.Override<Core.JurnalPembelian>(map =>
                                     map.References(x => x.Supplier).Column("SupplierId").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.JurnalPembelian>(map =>
                                    map.References(x => x.PurchaseOfficer).Column("KodeSalesman").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.JurnalPembelian>(
                map => map.HasMany(x => x.JurnalPembelianDetails).KeyColumn("NoVoucher").Cascade.All().Inverse().LazyLoad());
            
            model.Override<Core.JurnalPembelianDetail>(map => map.Id(x => x.JurnalPembelianDetailId).GeneratedBy.GuidComb());
            model.Override<Core.JurnalPembelianDetail>(map =>
                                      map.References(x => x.Departemen).Column("KodeDepartemen").Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.JurnalPembelianDetail>(map => map.References(x => x.JurnalPembelian).Column("NoVoucher").Not.Nullable().LazyLoad());

            model.Override<Core.KelompokAktiva>(map => map.Id(x => x.Kode));

            model.Override<Core.AktivaTetap>(map => map.Id(x => x.KodeAktivaTetap));
            model.Override<Core.AktivaTetap>(map => map.References(x => x.KelompokAktiva).Column("KodeKelompokAktiva").Not.Nullable().Fetch.Join().LazyLoad());
            model.Override<Core.AktivaTetap>(map => map.References(x => x.Departemen).Column("KodeDepartemen").Not.Nullable().Fetch.Join().LazyLoad());




            configuration = new Configuration();
            configuration.Configure(System.AppDomain.CurrentDomain.BaseDirectory + "/bin/" + _ConfigFileName);

           
            configuration.AddAutoMappings(model);
            //IDictionary<string, string> props = new Dictionary<string, string>();
            //props.Add("default_schema", "MSIACC_DB_NEW.dbo");
            //configuration.AddProperties(props);
            Initialize(configuration.BuildSessionFactory());

            //HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize();
        }

        public static void Initialize(ISessionFactory sessionFactory)
        {
            _sessionFactory = sessionFactory;
            //FileInfo t = new FileInfo(System.AppDomain.CurrentDomain.BaseDirectory + "/bin/Myddl.sql");
            //StreamWriter writer = t.CreateText();
            //new SchemaExport(configuration).Execute(true, false, false, _sessionFactory.OpenSession().Connection, writer); 
        }

        public static ISession OpenSession()
        {
            return _sessionFactory.OpenSession();
        }
    }
}